package de.nabble.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import de.nabble.Nabble;
import de.nabble.events.EventHandler;

public class MainFrame extends JFrame implements ActionListener {
	
	private BoardPanel panel;
	private ResourceBundle bundle;
	private EventHandler handler;
	private PointsPanel points;
	private RowPanel row;
	
	private static final String ACTION_EXIT = "exit";
	private static final String ACTION_CONNECT = "connect";
	private static final String ACTION_DISCONNECT = "disconnect";
	private static final String ACTION_SHOW_INFO = "showInfo";
	private static final String ACTION_NEW_GAME = "newGame";
	private static final String ACTION_CHECK = "check";
	
	public MainFrame(BoardPanel panel, PointsPanel points, RowPanel row) {
		this.panel = panel;
		this.bundle = ResourceBundle.getBundle(Nabble.RESOURCE_PATH);
		this.points = points;
		this.row = row;
		
		JButton check = createButton(getResource("Button.check"), 
				getResourceMnemonic("Button.check"), ACTION_CHECK);
		
		JPanel southPanel = new JPanel();
		LayoutManager l = new GridLayout(2, 1);
		southPanel.setLayout(l);
		southPanel.add(this.row);
		southPanel.add(check);
		
		add(this.panel);
		add(this.points, BorderLayout.EAST);
		add(southPanel, BorderLayout.SOUTH);
		
		setTitle(getResource("Nabble.title"));
		setJMenuBar(createMenuBar());
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/**
	 * Creates a button.
	 * @param name buttontext
	 * @param mnemonic buttonmnemonic
	 * @param actionCommand actionCommand
	 * @return the Jbutton with <code>this</code> as actionlistener.
	 */
	private JButton createButton(String name, char mnemonic, String actionCommand) {
		JButton result = new JButton(name);
		result.setMnemonic(mnemonic);
		result.setActionCommand(actionCommand);
		result.addActionListener(this);
		return result;
	}
	
	/**
	 * Creates the whole Menubar.
	 * @return the menubar.
	 */
	private JMenuBar createMenuBar() {
		JMenuBar menubar = new JMenuBar();
		
		JMenu file = createMenu(getResource("Menu.file"),
								getResourceMnemonic("Menu.file"));
		file.add(createMenuItem(getResource("MenuItem.exit"), 
								getResourceMnemonic("MenuItem.exit"),
								ACTION_EXIT));
		file.add(createMenuItem(getResource("MenuItem.new"),
								getResourceMnemonic("MenuItem.new"),
								ACTION_NEW_GAME));
		
		
		JMenu connection = createMenu(getResource("Menu.connection"),
									  getResourceMnemonic("Menu.connection"));
		connection.add(createMenuItem(getResource("MenuItem.connect"), 
									  getResourceMnemonic("MenuItem.connect"),
									  ACTION_CONNECT));
		connection.add(createMenuItem(getResource("MenuItem.disconnect"), 
									  getResourceMnemonic("MenuItem.disconnect"),
									  ACTION_DISCONNECT));
		
		JMenu help = createMenu(getResource("Menu.help"),
								getResourceMnemonic("Menu.help"));
		help.add(createMenuItem(getResource("MenuItem.info"), 
								getResourceMnemonic("MenuItem.info"),
								ACTION_SHOW_INFO));
		
		menubar.add(file);
		menubar.add(connection);
		menubar.add(help);
		
		return menubar;
	}

	/**
	 * invoked when an action occured.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals(ACTION_EXIT)) {
			handler.exit(0);
		} else if(command.equals(ACTION_CONNECT)) {
			handler.connect("");
		} else if(command.equals(ACTION_DISCONNECT)) {
			handler.disconnect();
		} else if(command.equals(ACTION_SHOW_INFO)) {
			handler.showInfo(this);
		} else if(command.equals(ACTION_NEW_GAME)) {
			handler.restart();
		} else if(command.equals(ACTION_CHECK)) {
			Nabble.log(handler.check());
		}
	}
	
	/**
	 * Creates a new JMenuItem
	 * @param title the text
	 * @param mnemonic a char value for invoking with alt mask
	 * @param actionCommand the actionCommand.
	 * @return the completet Jmenuitem with <code>this</code> as actionlistener.
	 */
	private JMenuItem createMenuItem(String title, char mnemonic, 
			String actionCommand) {
		JMenuItem item = new JMenuItem(title);
		item.setMnemonic(mnemonic);
		item.setActionCommand(actionCommand);
		item.addActionListener(this);
		return item;
	}
	
	/**
	 * Creates a new JMenu
	 * @param title the title
	 * @param mnemonic the mnemonic
	 * @return the jmenu
	 */
	private JMenu createMenu(String title, char mnemonic) {
		JMenu menu = new JMenu(title);
		menu.setMnemonic(mnemonic);
		
		return menu;
	}

	/**
	 * The resource for the given key.
	 * @param key the key of the resource
	 * @return the resource as string
	 */
	private String getResource(String key) {
		return bundle.getString(key).replace("&", "");
	}
	
	/**
	 * gets the mnemonic of a resource. The mnemonic should be marked with an '&'
	 * before itself in the resource file.
	 * @param key the key of the resource
	 * @return the mnemonic
	 */
	private char getResourceMnemonic(String key) {
		String res = bundle.getString(key);
		char c = res.charAt(res.indexOf('&') + 1);
		return c;
	}
	
	/**
	 * Sets the eventhandler for the application
	 * @param handler
	 */
	public void setHandler(EventHandler handler) {
		this.handler = handler;
	}
	
	private static final long serialVersionUID = -1889746225464122967L;
}